package com.tanhua.admin.service;

import com.alibaba.fastjson.JSON;
import com.bilibili.commons.Constants;
import com.bilibili.dubbo.api.db.UserInfoApi;
import com.bilibili.model.db.Freeze;
import com.bilibili.model.db.Unfreeze;
import com.bilibili.model.db.UserInfo;
import com.bilibili.model.vo.FreezeVo;
import com.bilibili.model.vo.PageResult;
import com.bilibili.model.vo.UnfreezeVo;
import com.bilibili.model.vo.UserInfoVoAge;
import com.tanhua.admin.interceptor.AdminHolder;
import com.tanhua.admin.mapper.FreezeMapper;
import com.tanhua.admin.mapper.UnFreezeMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author HuangRongFu
 * 2021/11/29 星期一8:28 上午
 */

@Service
public class ManageService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private FreezeMapper freezeMapper;

    @Autowired
    private UnFreezeMapper unFreezeMapper;

    @DubboReference
    private UserInfoApi userInfoApi;


    /**
     * 用户管理 分页查询
     *
     * @param page
     * @param pageSize
     * @Return: PageResult
     */
    public PageResult<UserInfoVoAge> findPage(Long page, Long pageSize) {

        PageResult pageResult = userInfoApi.findPage(page, pageSize);

        List<UserInfo> userInfoList = pageResult.getItems();

        List<UserInfoVoAge> voList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(userInfoList)) {

            for (UserInfo userInfo : userInfoList) {

                UserInfoVoAge vo = new UserInfoVoAge();

                BeanUtils.copyProperties(userInfo, vo);

                String freezeKey = Constants.FREEZE_USER + userInfo.getId().toString();
                if (stringRedisTemplate.hasKey(freezeKey)) {

                    vo.setUserStatus(Constants.USER_FREEZE_STATUS);//冻结状态
                }

                voList.add(vo);
            }
        }

        pageResult.setItems(voList);

        return pageResult;
    }

    /**
     * 查询用户详细
     *
     * @param userId
     * @Return: UserInfoVoAge
     */
    public UserInfoVoAge findById(Long userId) {

        UserInfo userInfo = userInfoApi.findById(userId);

        UserInfoVoAge vo = new UserInfoVoAge();

        String freezeKey = Constants.FREEZE_USER + userId.toString();
        BeanUtils.copyProperties(userInfo, vo);

        if (stringRedisTemplate.hasKey(freezeKey)) {

            vo.setUserStatus(Constants.USER_FREEZE_STATUS);//冻结状态
        }

        vo.setCreated(userInfo.getCreated().getTime());

        return vo;
    }

    /**
     * 冻结用户
     * @param freezeVo
     * @Return: Map
     */
    public Map<String, String> freezeUser(FreezeVo freezeVo) {

        Freeze freeze = new Freeze();
        BeanUtils.copyProperties(freezeVo,freeze);

        //获取操作人id
        Long adminId = AdminHolder.getUserId();

        //被冻结用户id
        Integer freezeUserId = freeze.getUserId();

        // 补全freeze信息
        freeze.setAdminId(adminId.intValue());
        freeze.setCreated(new Date());

        //存入redis
        String freezeKey = Constants.FREEZE_USER + freezeUserId.toString();

        //将冻结信息转化为json
        String freezeInfo = JSON.toJSONString(freeze);

        //冻结时间
        Integer freezingTime = freeze.getFreezingTime();

        //将冻结时间存入redis
        switch (freezingTime){

            case 1: stringRedisTemplate.opsForValue().set(freezeKey, freezeInfo, 3, TimeUnit.DAYS); break;
            case 2: stringRedisTemplate.opsForValue().set(freezeKey, freezeInfo, 7, TimeUnit.DAYS); break;
            case 3: stringRedisTemplate.opsForValue().set(freezeKey, freezeInfo); break;
            default: break;
        }

        freezeMapper.insert(freeze);

        Map<String, String> resultMap = new HashMap<>();

        resultMap.put("message", "冻结成功");

        return resultMap;
    }

    /**
     * 解冻用户
     * @param unfreezeVo
     * @Return: Map
     */
    public Map<String, String> unfreezeUser(UnfreezeVo unfreezeVo) {

        Unfreeze unFreeze = new Unfreeze();
        BeanUtils.copyProperties(unfreezeVo, unFreeze);

        Integer userId = unFreeze.getUserId();

        String key = Constants.FREEZE_USER + userId.toString();

        stringRedisTemplate.delete(key);

        unFreeze.setCreated(new Date());
        unFreeze.setAdminId(AdminHolder.getUserId().intValue());

        unFreezeMapper.insert(unFreeze);

        Map<String, String> resultMap = new HashMap<>();

        resultMap.put("message", "解冻成功");

        return resultMap;
    }
}
